package com.matt.repository.impl;

import com.dotflat.util.UuidUtil;
import com.matt.domain.ChildrenNurseryInfoRecord;
import com.matt.domain.courseV2.po.ChildrenCountPO;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.ExtendInfoRepository;
import com.matt.repository.base.BaseNativeSqlRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/9/11
 * *******************************
 */
@Component
public class ExtendInfoRepositoryRepositoryImpl extends BaseNativeSqlRepository implements ExtendInfoRepository {


    @Override
    public List<ChildrenNurseryInfoRecord> getChildrenIds(List<String> childrenIds, String beginAt, String endAt) {
        String ids = UuidUtil.getString(childrenIds);

        String sql = "SELECT cnir.childrenId,cnir.effectDay,DATE_FORMAT(cnir.beginDate, '%Y-%m-%d') as beginDate ,DATE_FORMAT(cnir.endDate, '%Y-%m-%d') as endDate" +
                " ,cnir.`status`,cnir.healthyStatus  " +
                " FROM  children c LEFT JOIN children_nursery_info_record  cnir on c.id = cnir.childrenId WHERE  " +
                " DATE_FORMAT(cnir.created,'%Y-%m-%d %H:%i:%S') >= DATE_FORMAT('" + beginAt + "','%Y-%m-%d %H:%i:%S')   " +
                " AND  DATE_FORMAT(cnir.created,'%Y-%m-%d %H:%i:%S') <= DATE_FORMAT('" + endAt + "','%Y-%m-%d %H:%i:%S')  " +
                "  and  c.id in (" + ids + ")";
        List<Object[]> objects = sqlArrayList(sql);
        List<ChildrenNurseryInfoRecord> objectArrayList = new ArrayList<>();
        for (int j = 0; j < objects.size(); j++) {
            ChildrenNurseryInfoRecord childrenNurseryInfoRecord = new ChildrenNurseryInfoRecord();
            childrenNurseryInfoRecord.setChildrenId(objects.get(j)[0].toString());
            childrenNurseryInfoRecord.setEffectDay(Integer.valueOf(objects.get(j)[1].toString()));
            childrenNurseryInfoRecord.setBeginDate(DateUtil.parseToDate(objects.get(j)[2].toString()));
            childrenNurseryInfoRecord.setEndDate(DateUtil.parseToDate(objects.get(j)[3].toString()));
            childrenNurseryInfoRecord.setStatus(Integer.valueOf(objects.get(j)[4].toString()));
            childrenNurseryInfoRecord.setHealthyStatus(Integer.valueOf(objects.get(j)[5].toString()));
            objectArrayList.add(childrenNurseryInfoRecord);
        }
        return objectArrayList;
    }

    @Override
    public List<ChildrenNurseryInfoRecord> findAllByChildrenClassId(String classId, String beginAt, String endAt,List<String> childrenIds) {
        String contractBeginDate = DateUtil.formatDayToStr(DateUtil.parseToDate(beginAt));
        String contractEndDate =  DateUtil.formatDayToStr(DateUtil.plusDaysToDate(DateUtil.parseToDate(endAt), 1));
        String ids = UuidUtil.getString(childrenIds);
        String sql = "SELECT cnir.childrenId," +
                "       cnir.created ," +
                "       cnir.effectDay," +
                "       cnir.beginDate," +
                "       cnir.endDate," +
                "       cnir.`status`," +
                "       cnir.healthyStatus " +
                "  FROM children_nursery_info_record cnir " +
                "  LEFT JOIN rel_classes_children rcc ON cnir.childrenId = rcc.childrenId " +
                " WHERE cnir.created>= '" + contractBeginDate + "'    " +
                "   AND cnir.created<= '" + contractEndDate + "' "
                + (StringUtils.isNotBlank(classId) ? String.format(" and rcc.`classesId` =  \"%s\" ", classId) : "")
                + (StringUtils.isNotBlank(ids) ? "AND cnir.childrenId in ("+ids+")" : "" )
                + " GROUP BY  cnir.childrenId,date_format(cnir.created,'%y%m%d') " ;

        List<ChildrenNurseryInfoRecord> objectArrayList = sqlObjectList(sql,ChildrenNurseryInfoRecord.class);

        return objectArrayList;
    }

    @Override
    public List<ChildrenCountPO> countAllEffectedDayByChildrenId(List<String> childrenIds, Date beginAt, Date endAt){

        if (beginAt == null || endAt == null){
            return null;
        }

        String sql = "SELECT cnir.`childrenId` ,COUNT(cnir.`effectDay`) as count " +
                "  FROM children_nursery_info_record cnir " +
                " WHERE cnir.created>= :1 " +
                "   AND cnir.created<= :2 " +
                "   AND cnir.childrenId in (:3) " +
                " and cnir.status in (1,2,5) GROUP BY cnir.`childrenId` ";

        List param = Arrays.asList(beginAt, endAt, childrenIds);

        List<ChildrenCountPO> objectArrayList = sqlObjectList(sql,param,ChildrenCountPO.class);

        return objectArrayList;
    }
}
